package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.mapper.TermMapper;
import com.nchu.student_score.model.Term;
import com.nchu.student_score.service.TermService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TermServiceImpl implements TermService {
    @Autowired
    TermMapper termMapper;

    @Override
    public ResultVo<Term> getTerms() {
        ResultVo<Term> resultVo = new ResultVo<>();
        resultVo.setList(termMapper.selectAll());
        return resultVo;
    }

    @Override
    public ResultVo<Term> updateTerm(Term term) {
        ResultVo<Term> resultVo = new ResultVo<>();

        if(term.getTermId() == null){
            termMapper.insert(term);
            resultVo.setStatus(0);
            resultVo.setMessage("新增学期成功!");
        }
        else{
            termMapper.updateByPrimaryKey(term);
            resultVo.setStatus(0);
            resultVo.setMessage("修改学期成功!");
        }

        return resultVo;
    }

    @Override
    public PageInfo<Term> getTermList(Integer currPage) {
        if(currPage == null) {
            currPage = 1;
        }
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(termMapper.selectAll());
    }
}
